package org.yeruyi.common.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import org.yeruyi.common.R;
import org.yeruyi.common.exception.UIParamsInvalidException;


/**
 * Author: yeruyi
 * Date: 2018/1/16
 * Version:v1.0
 * Scope:
 * 2018年1月16日 为了减少不必要的弹窗耗损时间，新建一个dialog模板
 */
public class CustomDialog extends Dialog {

    private static final String TAG = "[CustomDialog]";

    private builder mBuilder;

    private CustomDialog(Context context, builder builder) {
        super(context, R.style.custom_dialog);
        this.mBuilder = builder;
    }

    private CustomDialog(Context context, int style, builder builder) {
        super(context, style);
        this.mBuilder = builder;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
    }

    public static class builder {

        private int mDialogStyle;
        private boolean isNeedAlpha = false;
        private float mAlphaNum = 0.5f;
        private View mDialogView;
        private String mTitle;

        public builder setDialogView(View dialogView) {
            mDialogView = dialogView;
            return this;
        }

        public builder setTitle(String title) {
            mTitle = title;
            return this;
        }

        public builder setIsNeedAlpha(boolean isNeedAlpha) {
            isNeedAlpha = isNeedAlpha;
            return this;
        }

        public builder setAlphaNum(float alphaNum) {
            mAlphaNum = alphaNum;
            return this;
        }

        public builder setDialgStyle(int dialogStyle) {
            mDialogStyle = dialogStyle;
            return this;
        }

        public CustomDialog init(Context context) throws UIParamsInvalidException {
            if (mDialogView == null || (isNeedAlpha && mAlphaNum == 0)) {
                // dialog初始化时需要设置view
                throw new UIParamsInvalidException();
            }
            CustomDialog dialog = null;
            if (mDialogStyle != 0) {
                dialog = new CustomDialog(context, mDialogStyle, this);
            } else {
                dialog = new CustomDialog(context, this);
            }
            if (mTitle != null && !mTitle.isEmpty()) {
                dialog.setTitle(mTitle);
            }
            LinearLayout.LayoutParams params = new
                    LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            dialog.setContentView(mDialogView, params);
            if (isNeedAlpha) {
                Window window = dialog.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.alpha = mAlphaNum;
                window.setAttributes(lp);
            }
//            dialog.setContentView(DIALOG_VIEW);
            dialog.setCancelable(false);
            return dialog;
        }
    }
}
